package com.fxkj.cloud.drms.ui.mqtt.handler.receive;

import com.fxkj.cloud.dbs.common.constants.DeviceStatusEnum;
import com.fxkj.cloud.drms.application.DeviceApplicationService;
import com.fxkj.cloud.drms.infra.mq.mqtt.MqttConstants;
import com.fxkj.cloud.drms.ui.mqtt.handler.AbstractReceiveHandler;
import com.fxkj.cloud.drms.ui.mqtt.model.MqttClientOnlineVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * <p>description: </p>
 * @author ww
 * @date 2025/3/17 13:43
 */
@Component(MqttConstants.Topic.System.SYSTEM_DEVICE_ONLINE_SERVICE_NAME)
@Slf4j
public class SystemDeviceOnlineHandler extends AbstractReceiveHandler<MqttClientOnlineVO> {
    private final DeviceApplicationService deviceApplicationService;

    public SystemDeviceOnlineHandler(DeviceApplicationService deviceApplicationService) {
        this.deviceApplicationService = deviceApplicationService;
    }

    @Override
    protected void doHandler(String topic, MqttClientOnlineVO data, String... topicParams) throws Exception {
        log.info("[SystemDeviceOnlineHandler] -| 设备上线 topicParams: [{}], topic: {}, data: {}", topicParams, topic, data);
        String serialNo = topicParams[1];
        deviceApplicationService.deviceStatusChange(serialNo, DeviceStatusEnum.ONLINE);
    }

    @Override
    protected Class<MqttClientOnlineVO> getClazz() {
        return MqttClientOnlineVO.class;
    }

    @Override
    protected int indexNum() {
        return 2;
    }
}
